package handler

import (
	"github.com/gin-gonic/gin"
	"log"
	"task00/pkg/db"
	"task00/pkg/domain/osmData"
)

func (h *Handler) CreateRegion(c *gin.Context) {
	var region osmData.Region
	if err := c.BindJSON(&region); err != nil {
		c.JSON(400, gin.H{"error": "Invalid request body"})
		log.Println(err.Error())
		return
	}

	userId, _ := c.Cookie("id")
	result, err := h.service.PostOsmData(c.Request.Context(), userId, region)
	if err != nil {
		switch err.Error() {
		case db.ErrParamNotFound.Error():
			c.JSON(400, gin.H{"message": "params id can't be empty"})
			log.Println("params id can't be empty on GetAccount call")
			break
		case db.ErrAuthorize.Error():
			c.JSON(401, gin.H{"error": err.Error()})
			log.Println(err.Error())
			break
		case db.ErrNotExist.Error():
			c.JSON(404, gin.H{"error": "Parent region not found"})
			log.Printf("Parent region not found: %v", region.ParentRegion)
			break
		default:
			c.JSON(500, gin.H{"error": "Internal Server Error"})
			log.Println(err.Error())
		}
		return
	}

	c.JSON(201, result)
	log.Printf("Success Create Region %v", result)
	return
}
func (h *Handler) UpdateRegion(c *gin.Context) {
	id := c.Param("regionId")
	var region osmData.Region
	if err := c.BindJSON(&region); err != nil {
		c.JSON(400, gin.H{"error": "Invalid request body"})
		log.Println(err.Error())
		return
	}

	userId, _ := c.Cookie("id")
	result, err := h.service.PutOsmData(c.Request.Context(), userId, id, region)
	if err != nil {
		switch err.Error() {
		case db.ErrParamNotFound.Error():
			c.JSON(400, gin.H{"message": "params id can't be empty"})
			log.Println("params id can't be empty on GetAccount call")
			break
		case db.ErrAuthorize.Error():
			c.JSON(401, gin.H{"error": err.Error()})
			log.Println(err.Error())
			break
		case db.ErrNotExist.Error():
			c.JSON(404, gin.H{"error": "Region not found"})
			log.Printf("id:%v\terror:%v", id, err)
			break
		default:
			c.JSON(500, gin.H{"error": "Internal Server Error"})
			log.Println(err.Error())
		}
		return
	}

	c.JSON(200, result)
	log.Printf("Success Update Region %v", result)
	return
}
func (h *Handler) DeleteRegion(c *gin.Context) {
	id := c.Param("regionId")
	userId, _ := c.Cookie("id")
	err := h.service.DeleteOsmData(c.Request.Context(), userId, id)
	if err != nil {
		switch err.Error() {
		case db.ErrParamNotFound.Error():
			c.JSON(400, gin.H{"message": "params id can't be empty"})
			log.Println("params id can't be empty on GetAccount call")
			break
		case db.ErrAuthorize.Error():
			c.JSON(401, gin.H{"error": err.Error()})
			log.Println(err.Error())
			break
		case db.ErrNotExist.Error():
			c.JSON(404, gin.H{"error": "Region not found"})
			log.Printf("id:%v\terror:%v", id, err)
			break
		default:
			c.JSON(500, gin.H{"error": "Internal Server Error"})
			log.Println(err.Error())
		}
		return
	}

	c.JSON(204, nil)
	log.Printf("Success Delete Region %v", id)
	return
}
func (h *Handler) GetOsmData(c *gin.Context) {
	id := c.Param("regionId")
	userId, _ := c.Cookie("id")
	result, err := h.service.GetOsmData(c.Request.Context(), userId, id)
	if err != nil {
		switch err.Error() {
		case db.ErrParamNotFound.Error():
			c.JSON(400, gin.H{"message": "params id can't be empty"})
			log.Println("params id can't be empty on GetAccount call")
			break
		case db.ErrAuthorize.Error():
			c.JSON(401, gin.H{"error": err.Error()})
			log.Println(err.Error())
			break
		case db.ErrNotExist.Error():
			c.JSON(404, gin.H{"error": "Region not found"})
			log.Printf("id:%v\terror:%v", id, err)
			break
		default:
			c.JSON(500, gin.H{"error": "Internal Server Error"})
			log.Println(err.Error())
		}
		return
	}

	c.JSON(200, result)
	log.Printf("Success Get Region %v", result)
	return
}
